﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace KC.Presentation.WorkTool.Controls
{
	public partial class Db2Querier : UserControl
	{
		public string ConnectionString { get; private set; }

		public Db2Querier()
		{
			InitializeComponent();
		}

		private void tcQueries_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (tcQueries.SelectedTab.Name != "tpNew")
				return;

			TabPage tp = new TabPage();
			tp.Name = "tpQuery" + tcQueries.TabPages.Count.ToString();
			tp.Text = "Query " + tcQueries.TabPages.Count.ToString();
			Db2QuerierTabObject obj = new Db2QuerierTabObject(ConnectionString);
			obj.Dock = DockStyle.Fill;
			tp.Controls.Add(obj);
			tcQueries.TabPages.Insert(tcQueries.TabPages.Count - 1, tp);
			tcQueries.SelectedIndex = tcQueries.TabPages.Count - 2;
		}

		private void btnExecute_Click(object sender, EventArgs e)
		{
			ConnectionStringSettings css = (ConnectionStringSettings)ddlConnection.SelectedItem;
			if (css == null)
			{	// Build new connectionstring
				string username = "", password = "", database = "";

				AskQuestion aq = new AskQuestion("Connect to database: ");
				if (aq.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(aq.Answer))
					database = aq.Answer;
				else return;

				aq = new AskQuestion("Enter username: ");
				if (aq.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(aq.Answer))
					username = aq.Answer;
				else return;

				aq = new AskQuestion("Enter password: ", true);
				if (aq.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(aq.Answer))
					password = aq.Answer;
				else return;

				css = new ConnectionStringSettings(
					"Custom (" + database + " / " + username + ")",
					"Database=" + database +
					";UID=" + username + ";PWD="
					+ password + ";Server=d_pwbdb1:db2tcpipmain;");
			}

			ConnectionString = css.ConnectionString;
			ddlConnection.Items.Add(css);
			ddlConnection.SelectedItem = css;

			if (string.IsNullOrEmpty(ConnectionString))
				throw new ApplicationException("ConnectionString is invalid");
			foreach (TabPage tp in tcQueries.TabPages)
				foreach (Control c in tp.Controls)
					if (c.GetType() == typeof(Db2QuerierTabObject))
						((Db2QuerierTabObject)c).ConnectionString = ConnectionString;
			Db2QuerierTabObject obj = (Db2QuerierTabObject)tcQueries.SelectedTab.Controls[0];
			obj.Execute();
		}

		private void Db2Querier_Load(object sender, EventArgs e)
		{
			ddlConnection.Items.Clear();
			foreach (ConnectionStringSettings cs in ConfigurationManager.ConnectionStrings)
				ddlConnection.Items.Add(cs);
			ddlConnection.DisplayMember = "Name";
		}

		private void btnCloseTab_Click(object sender, EventArgs e)
		{
			tcQueries.TabPages.Remove(tcQueries.SelectedTab);
			tcQueries.SelectedIndex = tcQueries.TabPages.Count - 2;
		}
	}
}
